package controller;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Singer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.SingerService;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.http.HttpRequest;
import java.util.List;

@Controller
@RequestMapping("/singer")
public class SingerController {
    @Autowired
    private SingerService singerService;


    public void list (Model model, HttpServletRequest request){
        String pagecount = request.getParameter("pagecount");
        if (pagecount == null) {
            pagecount = "1";
        }
        PageHelper.startPage(Integer.parseInt(pagecount), 3);
        List<Singer> allSinger = singerService.getAllSinger();
        PageInfo pageInfo = new PageInfo(allSinger);
        int pages = pageInfo.getPages();
        if (Integer.parseInt(pagecount) >= pages) {
            pagecount = String.valueOf(pages);
        }
        PageHelper.startPage(Integer.parseInt(pagecount), 3);
        allSinger = singerService.getAllSinger();
        model.addAttribute("pagecount", pagecount);
        model.addAttribute("singerList", allSinger);
        model.addAttribute("singerList",allSinger);
    }


    @RequestMapping("singerList")
    public String SingerList (Model model, HttpServletRequest request){
        list(model, request);
        return "main";
    }


    @RequestMapping("singerDel")
    public String singerDel(Model model,HttpServletRequest request){
        String id = request.getParameter("id");
        int i = singerService.deleteSingerById(id);
        if (i == 1) {
            list(model, request);
            return "main";
        } else {
            list(model, request);
            return "main";
        }
    }

    @RequestMapping("singerAdd")
    public String singerAdd(Model model,HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        return "singerAdd";
    }

    @RequestMapping("singerAddSave")
    public String singerAddSave(Model model,HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String siNo = request.getParameter("SiNo");
        String siName = request.getParameter("SiName");
        String siArea = request.getParameter("SiArea");
        String siSex = request.getParameter("SiSex");
        String siRemark = request.getParameter("SiRemark");
        Singer singer = new Singer(siNo, siName, siArea, siSex, siRemark);
        int i = singerService.addSinger(singer);
        if (i == 1) {
            list(model, request);
            return "main";
        } else {
            list(model, request);
            return "main";
        }
    }


    @RequestMapping("singerDetail")
    public String singerDetail(Model model,HttpServletRequest request){
        String id = request.getParameter("id");
        Singer singer = singerService.getSingerById(id);
        request.setAttribute("singer", singer);
        return "singerDetail";
    }

    @RequestMapping("singerEdit")
    public String singerEdit(Model model,HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        Singer singer = singerService.getSingerById(id);
        request.setAttribute("singer", singer);
        return "singerEdit";
    }


    @RequestMapping("singerUpdate")
    public String singerUpdate(Model model,HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String siNo = request.getParameter("SiNo");
        String siName = request.getParameter("SiName");
        String siArea = request.getParameter("SiArea");
        String siSex = request.getParameter("gender");
        String siRemark = request.getParameter("SiRemark");
        Singer singer = new Singer(siNo, siName, siArea, siSex, siRemark);
        System.out.println(singer);
        int i = singerService.updateSinger(singer);
        if (i == 1) {
            list(model, request);
            return "main";
        } else {
            list(model, request);
            return "main";
        }
    }
}
